package com.spic.business.violation.supervision.information.controller;

import cn.hutool.core.util.ObjectUtil;
import com.spic.business.common.core.utils.poi.BusinessExcelUtil;
import com.spic.business.common.core.workflow.IWorkFlowService;
import com.spic.business.common.core.workflow.WorkFlowController;
import com.spic.business.violation.supervision.information.domain.ViolationInformation;
import com.spic.business.violation.supervision.information.service.IViolationInformationService;
import com.spic.common.core.utils.StringUtils;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.common.security.service.TokenService;
import com.spic.system.api.RemoteNoticeInfoService;
import com.spic.system.api.RemoteUserService;
import com.spic.system.api.domain.SysNoticeInfo;
import com.spic.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * 违章举报Controller
 *
 * @author spic
 * @date 2021-10-14
 */
@RestController
@RequestMapping("/violation/information")
public class ViolationInformationController extends WorkFlowController
{
    @Autowired
    private IViolationInformationService violationInformationService;
    @Autowired
    private RemoteNoticeInfoService remoteNoticeInfoService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private RemoteUserService remoteUserService;
    /**
     * 查询违章举报列表
     */
    @PreAuthorize(hasPermi = "business/violation:information:list")
    @GetMapping("/list")
    public TableDataInfo list(ViolationInformation violationInformation)
    {
        startPage();
        List<ViolationInformation> list = violationInformationService.selectViolationInformationList(violationInformation);
        return getDataTable(list);
    }

    /**
     * 通知安环部主任信息
     * 知会消息中需要带一点违章信息（违章人员、违章时间、违章区域、违章说明）
     */
    @PostMapping("/notice")
    public AjaxResult notice( @RequestBody ViolationInformation violationInformation) throws Exception {
        if (null == violationInformation || StringUtils.isEmpty(violationInformation.getDirector())){
            return error("安环部主任信息不可为空");
        }
        SysNoticeInfo sysNoticeInfo = new SysNoticeInfo();
        //当前时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String now = format.format(date);
        ViolationInformation informationServiceById = violationInformationService.getById(violationInformation.getId());
        //违章人员
        String responsible = remoteUserService.getUserById(new Long(violationInformation.getResponsible())).getData().getNickName();
        // 违章时间
        String illegalTime =format.format( informationServiceById.getIllegalTime());
        // 违章区域
        String illegalArea = informationServiceById.getIllegalArea();
        // 违章说明
        String illegalInstructions = "无";
        if (StringUtils.isNotEmpty(informationServiceById.getIllegalInstructions())){
            illegalInstructions = informationServiceById.getIllegalInstructions();
        }
        //举报人
        String createBy = remoteUserService.getUserById(new Long(informationServiceById.getCreateBy())).getData().getNickName();
        //当前节点处理人
        String safetyOfficer = "";
        // 获取当前的用户(举报当前节点处理人信息)
        LoginUser loginUser = tokenService.getLoginUser();
        if (ObjectUtil.isNotNull(loginUser)){
            safetyOfficer = loginUser.getSysUser().getNickName();
            sysNoticeInfo.setCreateBy(loginUser.getUserid().toString());
        }
        //通知信息
        String content = createBy + "发起的违章举报已由安环部安全员" + safetyOfficer + "处理</br>"
                + "违章人员: " + responsible + "</br>"
                + "违章时间: " + illegalTime + "</br>"
                + "违章区域: " + illegalArea + "</br>"
                + "违章说明: " + illegalInstructions + "</br>"
                +"安环部处理时间: " + now;
        sysNoticeInfo.setTitle("违章举报通知");
        sysNoticeInfo.setContent(content);
        sysNoticeInfo.setUserId(violationInformation.getDirector());
        sysNoticeInfo.setCreateTime(date);
        remoteNoticeInfoService.add(sysNoticeInfo);
        return success();
    }
    /**
     * 导出违章举报列表
     */
    @PreAuthorize(hasPermi = "business/violation:information:export")
    @Log(title = "违章举报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ViolationInformation violationInformation) throws IOException
    {
        List<ViolationInformation> list = violationInformationService.selectViolationInformationList(violationInformation);
        ExcelUtil<ViolationInformation> util = new BusinessExcelUtil<ViolationInformation>(ViolationInformation.class);
        util.exportExcel(response, list, "违章举报数据");
    }

    /**
     * 获取违章举报详细信息
     */
    @PreAuthorize(hasPermi = "business/violation:information:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(violationInformationService.selectViolationInformationById(id));
    }

    /**
     * 新增违章举报
     */
    @PreAuthorize(hasPermi = "business/violation:information:add")
    @Log(title = "违章举报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ViolationInformation violationInformation)
    {
        return AjaxResult.success(violationInformationService.insertViolationInformation(violationInformation));
    }

    /**
     * 修改违章举报
     */
    @PreAuthorize(hasPermi = "business/violation:information:edit")
    @Log(title = "违章举报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ViolationInformation violationInformation)
    {
        return toAjax(violationInformationService.updateViolationInformation(violationInformation));
    }

    /**
     * 删除违章举报
     */
    @PreAuthorize(hasPermi = "business/violation:information:remove")
    @Log(title = "违章举报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(violationInformationService.deleteViolationInformationByIds(ids));
    }
    //可按照 违章来源、违章时间对违章数据进行统计
    @GetMapping("/getillegalSourcesByTimes")
    public AjaxResult getillegalSourcesByTimes(ViolationInformation violationInformation)
    {
        List<HashMap<String, Object>> stringObjectHashMap = violationInformationService.getillegalSourceCount(violationInformation);
        return AjaxResult.success(stringObjectHashMap);
    }

    //可按照 违章类型、违章时间对违章数据进行统计
    @GetMapping("/getiillegalTypeByTimes")
    public AjaxResult getiillegalTypeByTimes(ViolationInformation violationInformation)
    {
        List<HashMap<String, Object>> stringObjectHashMap = violationInformationService.getillegalTypeCount(violationInformation);
        return AjaxResult.success(stringObjectHashMap);
    }

    //可按照 违章区域、违章时间对违章数据进行统计
    @GetMapping("/getillegalAreaByTimes")
    public AjaxResult getillegalAreaByTimes(ViolationInformation violationInformation)
    {
        List<HashMap<String, Object>> stringObjectHashMap = violationInformationService.getillegalAreaCount(violationInformation);
        return AjaxResult.success(stringObjectHashMap);
    }

    @Override
    protected IWorkFlowService getService() {
        return violationInformationService;
    }

    @GetMapping("downloadReport/{id}")
    public void  downloadReport(@PathVariable("id") Long id, HttpServletResponse response){
        violationInformationService.downloadReport(id, response);
    }

    @GetMapping("filterDirtyData")
    public AjaxResult filterDirtyData() {
        violationInformationService.filterDirtyData();
        return AjaxResult.success();
    }

    @GetMapping("createTrain")
    public AjaxResult createTrain() {
        violationInformationService.createTrain();
        return AjaxResult.success();
    }
}
